package com.yidu.ladder.delivery.service.impl;


import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.yidu.ladder.base.dao.RipareaDao;
import com.yidu.ladder.base.domain.Staff;
import com.yidu.ladder.base.domain.Subarea;
import com.yidu.ladder.delivery.dao.BusinessRecsiptDao;
import com.yidu.ladder.delivery.dao.WorkSheetImportDao;
import com.yidu.ladder.delivery.domain.BasicJobList;
import com.yidu.ladder.delivery.domain.BasicJobListVo;
import com.yidu.ladder.delivery.domain.JobList;
import com.yidu.ladder.delivery.domain.WorkSheetImport;
import com.yidu.ladder.delivery.domain.Workorder;
import com.yidu.ladder.delivery.service.WorkSheetImportService;
import com.yidu.ladder.util.AddressUtil;
import com.yidu.ladder.util.BeanUtil;
import com.yidu.ladder.util.ExcelUtils;
import com.yidu.ladder.util.IDGenerator;
import com.yidu.ladder.util.WorkOrderNewNo;

/**
 * 工作单导入业务实现类
 * @author bamboo
 *
 */
@Service
public class WorkSheetImportServiceImpl implements WorkSheetImportService{
	@Autowired
	private WorkSheetImportDao dao;
	
	@Autowired
	private RipareaDao ripareaDao;
	
	@Autowired
	private BusinessRecsiptDao businessRecsiptDao;
	/**
	 * 得到工作单导入dao
	 * @return 工作单导入dao
	 */
	public WorkSheetImportDao getDao() {
		return dao;
	}
	/**
	 * 设置工作单导入dao
	 * @param dao 工作单导入dao
	 */
	public void setDao(WorkSheetImportDao dao) {
		this.dao = dao;
	}
	/**
	 * 得到定区dao
	 * @return 定区dao
	 */
	public RipareaDao getRipareaDao() {
		return ripareaDao;
	}
	/**
	 * 设置定区dao
	 * @param ripareaDao 定区dao
	 */
	public void setRipareaDao(RipareaDao ripareaDao) {
		this.ripareaDao = ripareaDao;
	}
	/**
	 * 得到工作单dao
	 * @return 工作单dao
	 */
	public BusinessRecsiptDao getBusinessRecsiptDao() {
		return businessRecsiptDao;
	}
	/**
	 * 设置工作单dao
	 * @param businessRecsiptDao 工作单dao
	 */
	public void setBusinessRecsiptDao(BusinessRecsiptDao businessRecsiptDao) {
		this.businessRecsiptDao = businessRecsiptDao;
	}
	
	@Override
	public boolean saveJoblist(List<JobList> jobLists) {
		//创建派工单对象
		Workorder workorder = new Workorder();
		//遍历数据
		for (JobList jobList : jobLists) {
			//设置Id
			jobList.setJobListId(IDGenerator.getId());
		}
		//调用dao层添加方法，添加基础工作单信息
		int saveBasicJobList = dao.saveBasicJobList(jobLists);
		//添加包装费信息
		int saveChargingInfo = dao.saveChargingInfo(jobLists);
		//添加寄收人信息
		int savePostInfo = dao.savePostInfo(jobLists);
		//派工单Id
		workorder.setWorkBillId(IDGenerator.getId());
		//工单编号
		String workBillNo = WorkOrderNewNo.newNo("派工单", businessRecsiptDao
				.dateBusinessSizeQuery(new SimpleDateFormat("yyMMdd").format(System.currentTimeMillis())));
		//设置工作单编号
		workorder.setWorkBillNo(workBillNo);
		//业务受理Id
		workorder.setBusinessNoticeId(IDGenerator.getId());
		//受理单号
		String businessNoticeNo = WorkOrderNewNo.newNo("业务受理", businessRecsiptDao
				.dateBusinessSizeQuery(new SimpleDateFormat("yyMMdd").format(System.currentTimeMillis())));
		//设置受理单号
		workorder.setBusinessNoticeNo(businessNoticeNo);
		//派工单类型
		workorder.setBillType(0);
		//取件状态
		workorder.setPickState(2);
		//短信序号
		workorder.setSsmNumber(0);
		//工单生产时间
		workorder.setWorkBillTime(new Date(System.currentTimeMillis()));
		//追单次数
		workorder.setTraceNum(0);
		//定义职员名
		String staffName = null;
		//定义地址
		String address = null;
		//遍历数据得到取派员名
		for (JobList jobList : jobLists) {
			//拿到取件职员名
			staffName = jobList.getGetProductManName();
			//拿到收货人地址
			address = jobList.getSenderAddress();
		}
		//查询职员相关信息
		Staff staff = businessRecsiptDao.findStaffByName(staffName);
		//设置职员Id
		workorder.setStaffId(staff.getStaffId());
		//给派工单对象设置职员编号
		workorder.setStaffNumber(staff.getStaffNumber());
		//给派工单对象设置职员名
		workorder.setStaffName(staff.getStaffName());
		//给派工单对象设置组织名
		workorder.setTakeOrgName(staff.getOrgName());
		//给派工单对象设置取派时间
		workorder.setTakeTime(new Date(System.currentTimeMillis()));
		
		//调用地址工具类解析地址得到省市县
		Map<String, Object> map = AddressUtil.separateAddress(address);
		//解析地址得到省的字符串
		String provinces = map.get("province").toString();
		//解析地址得到市的字符串
		String citys = map.get("city").toString();
		//解析地址得到县的数据
		String districts = map.get("district").toString();
		//调用地址解析工具类将得到的省市县首字母大写
		String addressCode = AddressUtil.upperAddress(provinces, citys, districts);
		System.out.println(addressCode);
		//根据地区编码地址查询定区信息
		Subarea subarea = businessRecsiptDao.findCityCodeByQuery(addressCode);
		//给派工单对象设置定区Id
		workorder.setRipAreaId(subarea.getRipAreaId());
		//添加派工单信息
		businessRecsiptDao.addWorkorder(workorder);
		//判断添加结果
		if (saveBasicJobList==1 && saveChargingInfo==1 && savePostInfo==1) {
			return true;
		}else {
			return false;
		}
	}
	
	@Override
	public List<WorkSheetImport> importUpload(InputStream in, MultipartFile file) throws Exception {
		//创建集合对象读取Excel文件
		List<List<Object>> listob = null;
        try {
            listob = new ExcelUtils().getBankListByExcel(in,file.getOriginalFilename());
        } catch (Exception e) {
            e.printStackTrace();
        }
		//创建工作单导入集合接收文件
		List<WorkSheetImport> sheetImports = new ArrayList<WorkSheetImport>();
		//遍历listob数据
		for(int i=1;i<listob.size();i++) {
			//获得Excel中元素
			List<Object> lo = listob.get(i);
			//判断Excel中元素是否不为空
			if(!lo.isEmpty()) {
				//调用解析工具类中的方法将获得元素集合放入转换为工作单实体
				WorkSheetImport workSheetImport = BeanUtil.convertListToBean(lo, WorkSheetImport.class);
				//将集合添加到实体对象
				sheetImports.add(workSheetImport);
			}
			
		}
		//返回实体类对象集合
		return sheetImports;
	}

	@Override
	public Map<String, Object> lazySelectjobList(BasicJobListVo basicJobListVo) {
		//设置页面
		basicJobListVo.setPage((basicJobListVo.getPage() - 1) * basicJobListVo.getLimit());
		//调用Dao层方法分页查询
		List<Map<String, Object>> list = dao.lazyLoadBasicJobist(basicJobListVo);
		//每次查询得到相应的记录数
		int count = dao.countJoblist(basicJobListVo);
		//创建一个Map接收数据返回给前端
		Map<String, Object> map = new HashMap<String, Object>();
		//map存放code
		map.put("code", 0);
		//map存放count
		map.put("count", count);
		//map存放数据
		map.put("data",list);
		//返回一个map
		return map;
	}

	@Override
	public List<BasicJobList> setJobListNo(String num) {
		//创建基础工作单集合
		List<BasicJobList> list = new ArrayList<BasicJobList>();
		//拿到的数据用，分开
		String[] nums = num.split(","); 
		//变量集合给工作单编号赋值
		for (int i = 0; i < nums.length; i++) {
			//运用工具类生成工作单编号
			String jobListNo = WorkOrderNewNo.newNo("工作单", nums.length);
			//创建基础工作单对象
			BasicJobList basicJobList = new BasicJobList();
			//基础工作单修改工作单编号
			basicJobList.setJobListNo(jobListNo);
			//将工作单编号添加到集合中
			list.add(basicJobList);
		}
		//返回基础工作单集合
		return list;
	}

	@Override
	public List<Staff> selectStaffByCode(String mnemoniCode) {
		//查询分区表的相关信息
		Subarea subarea = dao.findSubAreaMsgBycode(mnemoniCode);
		//调用定区方法中的查询职员的方法
		List<Staff> list = ripareaDao.findAllStaffByRipAreaId(subarea.getRipAreaId());
		return list;
	}

	
}
